<?php
class hrd_lembur_model extends MY_Model {
	var $nama_tabel = 'hrd_lembur';
	
	function GetJSON($idKaryawan, $idPeriode) {
		$ls_sql	= "	SELECT	to_char(a.tgl_lembur, 'dd/mm/yyyy') tgl_lembur, to_char(a.tgl_absen, 'dd/mm/yyyy') tgl_absen, b.nama_jenis_lembur, a.jam_mulai, a.jam_selesai, to_char(a.lama_jam_lembur, 999.99) lama_jam_lembur, to_char(a.jumlah, 999.99) jumlah, a.nilai, a.uang_makan, a.ket_lembur, a.id_lembur_pk, a.no_spl, a.id_periode_fk
				FROM	".$this->nama_tabel." a
				LEFT JOIN hrd_jenis_lembur b ON a.id_jenis_lembur_fk = b.id_jenis_lembur_pk
				WHERE 	a.id_karyawan_fk = '".$idKaryawan."'
				AND a.id_periode_fk = '".$idPeriode."'
				ORDER BY tgl_lembur";

		$list = $this->db->query($ls_sql)->result_array();

		// Ambil Jumlah
		$this->db->select_sum('nilai');
		$this->db->select_sum('uang_makan');
		$this->db->where('id_karyawan_fk', $idKaryawan);
		$this->db->where('id_periode_fk', $idPeriode);
		$sum = $this->db->get('hrd_lembur')->row_array();
		$footer[] = array('jumlah' => '<b>Total</b>', 'nilai' => $sum['nilai'], 'uang_makan' => $sum['uang_makan']);
		$data = array('total' => count($list), 'rows' => $list, 'footer' => $footer);
		// $data = array('total' => count($list), 'rows' => $list);

		return json_encode( $data );
		// print_r($sum);
	}

	function Add($arr_data){
		$this->db->insert($this->nama_tabel, $arr_data);
	}

	function Update($arr_data, $arr_rule){
		$this->db->update($this->nama_tabel, $arr_data, $arr_rule);
	}

	function DeleteSingle($arr_rule) {
		try {
			$this->db->delete($this->nama_tabel, array('id_lembur_pk' => $arr_rule['id_lembur_pk']));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	function GenerateId() {
		$sql = "select * 
				from ( select * 
				        from hrd_lembur
				        order by id_lembur_pk desc
				        )
				where ROWNUM <= 1
				";
		$data = $this->db->query($sql)->row_array();

		if (count($data) == 0) {
			return 1;
		} else {
			return $data['id_lembur_pk']+1;
		}
	}

}
?>